function select(probability) {
    var total = 0;
    for(var p in probability) {
        total += probability[p];
    }
    var r = Math.floor(Math.random() * total);

    var n = 0;
    for(var p in probability) {
        n += probability[p];
        if (r < n) {
            return p;
        }
    }
    return 0;
}

function getX(node) {
    var res = 0;
    while(node != null) {
      res += node.offsetLeft;
      node = node.offsetParent;
    }
    return res;
}

function getY(node) {
    var res = 0;
    while(node != null) {
      res += node.offsetTop;
      node = node.offsetParent;
    }
    return res;
}
